package com.mvp.shq.washer.ui.main;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.mvp.shq.washer.APP;
import com.mvp.shq.washer.entity.BaseResult;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.entity.VersionBean;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.http.RxSchedulers;
import com.mvp.shq.washer.jpush.TagAliasOperatorHelper;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/19.
 */

public class MainPresenter extends MainConstract.Presenter {
    @Override
    public void onStart() {
        getVersion();
        if (SpUtil.getUser() != null){
            setAlias();
        }
    }

    @Override
    void setAlias() {
        int sequence = 0;
        String alias = SpUtil.getUser().getId();
        TagAliasOperatorHelper.TagAliasBean bean = new TagAliasOperatorHelper.TagAliasBean();
        bean.setAliasAction(true);
        bean.setAlias(alias);
        bean.setAction(TagAliasOperatorHelper.ACTION_SET);
        TagAliasOperatorHelper.getInstance().handleAction(APP.getAppContext(),sequence,bean);
//        Observable.create(new ObservableOnSubscribe<String>() {
//            @Override
//            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
//                JPushInterface.setAlias(APP.getAppContext(),sequence,alias);
//            }
//        }).subscribeOn(Schedulers.newThread())
//                .subscribe(new Consumer<String>() {
//                    @Override
//                    public void accept(@NonNull String s) throws Exception {
//
//                    }
//                });
    }

    @Override
    void getUserInfo() {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        Logger.d("token："+token);
        Logger.d("userId:"+userId);
        model.getUserInfo(token,userId)
                .subscribe(new BaseObserver<User>(view) {
                    @Override
                    protected void onSuccees(BaseResult<User> t) throws Exception {
                        if (!t.isError()){
                            SpUtil.setUser(t.getData());
                            RxBus.get().send(EventCode.UPDATE_USER);
                        }else {
                            T.show(t.getMsg());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show("获取用户信息失败");
                        e.printStackTrace();
                        Logger.e("获取用户信息失败:");
                    }
                });
    }

    @Override
    void getVersion() {
        String platform = "0";
        model.version(platform).subscribe(new BaseObserver<VersionBean>(view) {
            @Override
            protected void onSuccees(BaseResult<VersionBean> t) throws Exception {
                if (!t.isError()){
                    PackageManager manager = APP.getApp().getPackageManager();
                    PackageInfo info = null;
                    try {
                        info = manager.getPackageInfo(APP.getApp().getPackageName(), 0);
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                    String version = info.versionName;
                    if (!version.equals(t.getData().getVersionName())){
                        view.updateAPK(t.getData());
                    }
                }
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

            }
        });
    }
}
